package com.shanling.shanlingcontroller.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.shanling.shanlingcontroller.CustomApplication;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.Music;
import com.shanling.shanlingcontroller.music.player.PlayManager;
import com.shanling.shanlingcontroller.music.player.PlaylistLoader;
import com.shanling.shanlingcontroller.music.player.SongLoader;
import com.shanling.shanlingcontroller.utils.ConvertUtils;
import com.shanling.shanlingcontroller.utils.CoverLoader;
import com.shanling.shanlingcontroller.utils.DpUtils;
import com.shanling.shanlingcontroller.utils.ScanFileHelper;
import com.shanling.shanlingcontroller.utils.ToastUtils;
import com.shanling.shanlingcontroller.music.event.PlaylistEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ch1 on 2019/11/12.
 */

public class MusicPopupWindow {
    private PopupWindow popupWindow;
    private  View popupView;
    private Activity activity;
    public MusicPopupWindow(Activity activity) {
        this.activity=activity;
        initPopwwindow(activity);
    }
    private void initPopwwindow(Context mContext) {
        popupView = LayoutInflater.from(mContext).inflate(R.layout.layout_musicpop, null);
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, DpUtils.dpToPx( mContext,350), true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.PopupAnimation);
        popupWindow.setBackgroundDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.bg_normal)));
        popupWindow.getContentView().setFocusableInTouchMode(true);
        popupWindow.getContentView().setFocusable(true);
    }
    public  void showPopwindow(Music music,View view){
        if(popupView!=null&&music!=null&&view!=null){
            ImageView iv_cover=popupView.findViewById(R.id.iv_cover);
            TextView tv_song_name=popupView.findViewById(R.id.tv_song_name);
            TextView tv_albumname=popupView.findViewById(R.id.tv_albumname);
            TextView tv_next=popupView.findViewById(R.id.tv_next);
            TextView tv_Collection=popupView.findViewById(R.id.tv_Collection);
            TextView tv_ablum=popupView.findViewById(R.id.tv_ablum);
            TextView tv_singer=popupView.findViewById(R.id.tv_singer);
            TextView tv_delete=popupView.findViewById(R.id.tv_delete);
            CoverLoader.INSTANCE.loadImageView(activity, music.getCoverUri(),iv_cover);
            tv_song_name.setText(ConvertUtils.getTitle(music.getTitle()));
            tv_albumname.setText(ConvertUtils.getArtist(music.getArtist()));
            tv_ablum.setText(activity.getString(R.string.album)+": "+ConvertUtils.getAlbum(music.getAlbum()));
            tv_singer.setText(activity.getString(R.string.singer)+": "+ConvertUtils.getArtist(music.getArtist()));
            tv_next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Music playingMusic = PlayManager.getPlayingMusic();
                    if(music.getMid().equals(playingMusic.getMid())){
                        ToastUtils.showToast(CustomApplication.getInstance(),R.string.Playing_thissong);
                    }else {
                         ToastUtils.showToast(CustomApplication.getInstance(),R.string.Added_successfully);
                         PlayManager.nextPlay(music);
                    }
                    popupWindow.dismiss();
                }
            });
            tv_Collection.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<Music> songs = SongLoader.INSTANCE.getFavoriteSong();
                    if(!songs.contains(music)){
                       SongLoader.INSTANCE.updateFavoriteSong(music);
                        EventBus.getDefault().post(new EventCenter(Constants.PLAYLISTEVENT, new PlaylistEvent(Constants.PLAYLIST_LOVE_ID, null)));
                    }
                    ToastUtils.showToast(CustomApplication.getInstance(),R.string.collected_song);
                    popupWindow.dismiss();
                }
            });
            tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PlaylistLoader.INSTANCE.deleteLocalMusic(activity,music);
                    if(listener!=null){
                        listener.onDelete(music);
                    }
                    popupWindow.dismiss();
                }
            });
            popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
            bgAlpha(0.4f,activity);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    bgAlpha(1.0f,activity);
                }
            });
        }
    }

    public void dissmiss(){
        if(popupWindow.isShowing()){
            popupWindow.dismiss();
        }
        popupWindow=null;
    }

    private  void bgAlpha(float f,Activity activity){
        WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
        layoutParams.alpha = f;
        activity.getWindow().setAttributes(layoutParams);
    }

    /**
     * 定义一个接口
     */
    public interface   onListener{
        void onDelete(Music music);
    }

    /**
     *定义一个变量储存数据
     */
    private  onListener listener;

    /**
     *提供公共的方法,并且初始化接口类型的数据
     */
    public void setListener( onListener listener){
        this.listener =  listener;
    }



}
